﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace market
{
    public partial class AdminAdd : System.Web.UI.Page
    {
        CommonClass ccObj = new CommonClass();
        DBClass dbObj = new DBClass();
        protected void Page_Load(object sender, EventArgs e)
        {
            check_Login();
        }
        public void check_Login()
        {
            if ((Session["AName"] == null))
            {
                Response.Write("<script>alert('对不起！您不是管理员，无权限浏览此页！');location='Login.aspx'</script>");
                Response.End();
            }
        }
        protected void btnCancel_Click(object sender, EventArgs e)
        {
            this.txtName.Text = "";
            this.txtPassWord.Text = "";
            this.txtTrueName.Text = "";
            this.txtEmail.Text = "";
        }
        protected void btnSave_Click(object sender, EventArgs e)
        {
            string strSql = "select * from Admin where AdminName='" + this.txtName.Text.Trim() + "'";
            DataTable dsTable = dbObj.GetDataSetStr(strSql, "tbAdmin");
            if (dsTable.Rows.Count > 0)
            {
                Response.Write(ccObj.MessageBoxPage("该用户名已存在！"));
            }
            else
            {
                string strsql = "select count(OrderID) from OrderInfo";
                int ID = Convert.ToInt32(dbObj.ExecScalar(dbObj.GetCommandStr(strsql))) + 1;
                string dt = this.txtDate.Value.ToString();
                DateTime.TryParse(dt, out DateTime dateTime);
                string strName = this.txtName.Text.Trim();
                string strPwd = this.txtPassWord.Text.Trim();
                string strTrueName = this.txtTrueName.Text.Trim();
                string strEamil = this.txtEmail.Text.Trim();
                string strAddSql = "Insert into Admin(AdminID,AdminName,PassWord,RealName,Email,LoadDate)";
                strAddSql += "values('" + ID + "','" + strName + "','" + strPwd + "','" + strTrueName + "','" + strEamil + "','" + dateTime + "')";
                SqlCommand myCmd = dbObj.GetCommandStr(strAddSql);
                dbObj.ExecNonQuery(myCmd);
                Response.Write(ccObj.MessageBoxPage("添加成功！"));
            }
        }
    }
}